<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <link rel="stylesheet" href="/static/layui/css/layui.css">
</head>
<body>
  <div>
    <span class="layui-breadcrumb">
      <a href="/dashboard">仪表盘</a>
      <a><cite>商品管理</cite></a>
    </span>
  </div>
  <a href="/itemAdd" style="margin-top:30px" class="layui-btn layui-btn-warm">增加</a>
  <table class="layui-table">
    <thead>
      <tr>
        <th>商品名字</th>
        <th>商品价格</th>
        <th>商品描述</th>
        <th>上下架</th>
        <th>库存</th>
        <th>商品封面</th>
        <th>操作</th>
      </tr> 
    </thead>
    <tbody>
      <% for(var i=0;i<items.length;i++) {%>
      <tr>
        <td><%= items[i].itemName %></td>
        <td><%= items[i].itemPrice %></td>
        <td title="<%= items[i].itemDesc %>"><%= items[i].itemDesc ? items[i].itemDesc.length>10?items[i].itemDesc.substr(0,10)+'...':items[i].itemDesc  :'暂无描述' %></td>
        <td class="layui-form">
          <input type="checkbox" disabled <%= items[i].onSale === 0? 'checked': ''  %> name="zzz" lay-skin="switch" lay-text="上架|下架">
        </td>
        <td>
          <%= items[i].itemStore %>
        </td>
        <td>
          <img width="100" src="<%=items[i].itemThumb?items[i].itemThumb:'https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fj1.58cdn.com.cn%2Fdist%2Fjxedt%2Fpc%2Fproducts%2Fjxregister%2Fimages%2Fno_data.png&refer=http%3A%2F%2Fj1.58cdn.com.cn&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1638081968&t=28423e640f83bc869db6c50a54d63527' %>" alt="">
        </td>
        <td>
          <a href="/itemUpdate?id=<%= items[i]._id %>" class="layui-btn layui-btn-xs">编辑</a>
          <button data-id="<%= items[i]._id %>" class="del layui-btn layui-btn-danger layui-btn-xs">删除</button>
        </td>
      </tr>
      <%}%>
    </tbody>
  </table>
  <div id="page"></div>    
  <script src="/static/layui/layui.js"></script>
  <script>
    layui.use(['laypage', 'jquery', 'layer'], function(){
      var laypage = layui.laypage;
      var $ = layui.jquery;
      var layer = layui.layer;

      // 删除商品
      $('.del').click(function(){
        var _this = this;
        layer.confirm('删除商品?', {icon: 3, title:'您确认要删除吗'}, function(index){
          $.ajax({
            url: '/delItem',
            data: {
              id: _this.dataset.id
            },
            success: function(res){
              if(res.code === 0 ) {
                layer.msg(res.msg, {
                  icon: 1,
                  time: 1000
                }, function(){
                  // 刷新
                  history.go(0)
                })
              }else{
                layer.msg(res.msg, {
                  icon: 2,
                  time: 1000
                })
              }
            }
          })
          layer.close(index);
        });
      })

      // 渲染分页
      laypage.render({
        elem: 'page' //注意，这里的 test1 是 ID，不用加 # 号
        ,count: <%= count %> //数据总数，从服务端得到
        ,limit: 2
        ,curr: <%= page %> // 当前页
        ,jump: function(obj, first){
          // 点击切换也是回调
          // first代表是否首次打开 
          if(!first){
            location.href="/itemLists?page="+obj.curr+"&pageSize="+obj.limit;
          }
        }
      });
    })
  </script>
</body>
</html>